Adding HTTPS to a Website with Nginx

Plain HTTP traffic can be captured and read, and an intermediary can alter its contents. Chrome also marks sites without valid HTTPS as insecure. HTTPS protects the contents in transit and makes tampering detectable, so I wanted to enable it for my site.

The overall process is obtain an SSL certificate -> deploy it to Nginx -> set up an HTTP-to-HTTPS redirect (optional) -> configure Cloudflare (optional).

Obtain a certificate for your domain

HTTPS uses a certificate to verify the site as an encrypted connection is established. There are plenty of detailed explanations of HTTPS and SSL online; here I will focus on getting a certificate and configuring the site.

I used Let’s Encrypt, a free certificate authority. During the request, it checks that you control the domain for which you want a certificate.

First install Certbot with sudo apt-get install certbot. Certbot can also handle certificate renewals.

Run sudo certbot --manual --preferred-challenges dns certonly. After you agree to the prompt about recording your IP address, Certbot displays a DNS challenge like this:

1
2
3
4
5
6
7
8
9
10
-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.example.com with the following value:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX(你的token)

Once this is deployed,
-------------------------------------------------------------------------------
Press Enter to Continue

Certbot pauses while you set up the challenge. Open your domain’s DNS management page and create a TXT record named _acme-challenge.yourdomain.xxxx, using the token shown above as its value.

Give the DNS change some time to propagate, then check for the token with dig -t txt _acme-challenge.yourdomain.xxxx. I ran this on Linux; my Windows 10 machine did not have the dig command.

Once the record is visible, return to Certbot on the server and press Enter. If verification succeeds, the files appear under /etc/letsencrypt/live/yourdomain.xxxx/. The two files I generally use are cert.pem, the certificate, and privkey.pem, the private key.

The files in live are links, much like Windows shortcuts. You can point Nginx at them directly. If you need to move the certificate, the underlying files are in /etc/letsencrypt/archive/yourdomain.xxxx/ and have slightly different names.

Deploy to Nginx

Next, tell Nginx where to find the certificate and which local service to proxy. Here is the configuration I used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server{
listen 443 ssl; # https默认监听443端口
listen [::]:443 ssl;
ssl_certificate /home/damaoooo/damaoooo.cf/cert1.pem; # 这里是你的证书的位置
ssl_certificate_key /home/damaoooo/damaoooo.cf/privkey1.pem; # 这里是你密钥的位置
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
server_name blog.damaoooo.cf; # 这里是你nginx要转发的域名


location / {
proxy_pass http://127.0.0.1:4400; # 这里是网站的原始位置
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}

Save the configuration under /etc/nginx/conf.d with a name such as xxxx.conf. Check its syntax and reload Nginx:

1
2
nginx -t //用于检查是否有语法错误
nginx -s reload

You should then be able to visit the site at an address such as https://abc.yourdomain.xxxx.

Force redirects to HTTPS

A browser request to the ordinary HTTP address uses port 80. To avoid typing https:// each time, add a server configuration that redirects http://abc.yourdomain.xxx to https://abc.yourdomain.xxx:

1
2
3
4
5
6
7
8

server {
listen 80;
listen [::]: 80;
server_name abc.yourdomain.xxxx;# 你要转换的网址

rewrite ^(.*)$ https://$host$1 permanent; # 这个不要动
}

Again, run nginx -t to check the configuration, then nginx -s reload to apply it.

Configure Cloudflare

Cloudflare can put a CDN in front of the site. In the default SSL setup I encountered, the browser-to-CDN connection used HTTPS, but the CDN-to-origin connection used HTTP. For HTTPS on both legs, enable the cloud for the DNS record and set Cloudflare’s SSL mode to full(strict). The official documentation explains the options:

Set up end-to-end HTTPS

If the browser reports too many redirects, check whether the forced HTTPS redirect is enabled while the CDN is still connecting to the origin over HTTP.

That completes the HTTPS setup.